package com.lanou.zy.projecta.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.application.BaseApplication;
import com.lanou.zy.projecta.entity.CountryMsg;
import com.lanou.zy.projecta.entity.OtherCountry;
import com.lanou.zy.projecta.volley.VolleySingleton;
import com.lanou.zy.projecta.entity.CheapTravel;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class CheapTravelListViewAdapter extends BaseAdapter {
    private ImageLoader imageLoader;
    private List<CountryMsg.DataEntity.NewDiscountEntity> entities;
    private List<OtherCountry.DataEntity.NewDiscountEntity> newDiscountEntities;
    private Context context;
    private BaseApplication app;

    public CheapTravelListViewAdapter(Context c) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
        this.context = c;
        this.app = (BaseApplication) ((Activity) context).getApplication();
    }

    public void addData(List<CountryMsg.DataEntity.NewDiscountEntity> datas) {
        this.entities = datas;
        notifyDataSetChanged();
    }

    public void addDatas(List<OtherCountry.DataEntity.NewDiscountEntity> datas) {
        this.newDiscountEntities = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (app.isType()) {
            if (newDiscountEntities != null && newDiscountEntities.size() > 6) {
                return 6;
            } else if (newDiscountEntities != null && newDiscountEntities.size() <= 6) {
                return newDiscountEntities.size();
            }
            return 0;
        } else {
            if (entities != null && entities.size() > 6) {
                return 6;
            } else if (entities != null && entities.size() <= 6) {
                return entities.size();
            }
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        if (app.isType()) {
            return newDiscountEntities.get(position);
        } else {
            return entities.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CheapTravelViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.cheaptravel_listview_item, null);
            holder = new CheapTravelViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (CheapTravelViewHolder) convertView.getTag();
        }

        if (app.isType()) {

            OtherCountry.DataEntity.NewDiscountEntity odn = newDiscountEntities.get(position);
            holder.cheapTravelDiscount_Tv.setText(odn.getPriceoff());
            holder.cheapTravelTitle_Tv.setText(odn.getTitle());
            holder.cheapTravelPrice_Tv.setText(odn.getPrice().replace("<em>", "").replace("</em>", ""));
            holder.cheapTravel_Iv.setImageUrl(odn.getPhoto(), imageLoader);

            return convertView;
        } else {

            CountryMsg.DataEntity.NewDiscountEntity c = entities.get(position);

            holder.cheapTravelDiscount_Tv.setText(c.getPriceoff());
            holder.cheapTravelTitle_Tv.setText(c.getTitle());
            holder.cheapTravelPrice_Tv.setText(c.getPrice().replace("<em>", "").replace("</em>", ""));
            holder.cheapTravel_Iv.setImageUrl(c.getPhoto(), imageLoader);
            return convertView;
        }


    }

    public class CheapTravelViewHolder {
        private NetworkImageView cheapTravel_Iv;
        private TextView cheapTravelTitle_Tv, cheapTravelDiscount_Tv, cheapTravelPrice_Tv;

        public CheapTravelViewHolder(View v) {
            cheapTravel_Iv = (NetworkImageView) v.findViewById(R.id.cheapTravel_Iv);
            cheapTravelPrice_Tv = (TextView) v.findViewById(R.id.cheapTravelPrice_Tv);
            cheapTravelTitle_Tv = (TextView) v.findViewById(R.id.cheapTravelTitle_Tv);
            cheapTravelDiscount_Tv = (TextView) v.findViewById(R.id.cheapTravelDiscount_Tv);
        }
    }

}
